/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.qsoft.bean;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.qsoft.general.XLS;

/**
 *
 * @author Jonathan
 */
@ManagedBean
@SessionScoped
public class FileUpLoadControl {

    private String destination = "c:\\tmppp";
    private UploadedFile file;
    private String cell1;

    public FileUpLoadControl() {
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
 public String getCell1() {
        return cell1;
    }

    public void setCell1(String cell1) {
        this.cell1 = cell1;
    }
    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile file2 = event.getFile();
        file=file2;
         String extValidate;
        if (getFile() != null) {
            String ext = getFile().getFileName();
            if (ext != null) {
                extValidate = ext.substring(ext.indexOf(".") + 1);
            } else {
                extValidate = "null";
            }
            if (extValidate.equals("xls")) {
                try {
                    TransferFile(getFile().getFileName(), getFile().getInputstream());
                } catch (IOException ex) {
                    Logger.getLogger(FileUpLoadControl.class.getName()).log(Level.SEVERE, null, ex);
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, new FacesMessage("Wrong", "Error uploading file"));
                }
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Succesful", getFile().getFileName() + " is upload. typecontent" + getFile().getContentType() + " size " + getFile().getSize()));
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Worng_ext", "only extension .pdf"));
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Worng", "s file!!"));
        }
    }

    public void TransferFile(String fileName, InputStream in) {
        try {
            XLS obj = new XLS();
            OutputStream out = new FileOutputStream(new File(destination + fileName));
            
            int reader = 0;
            byte[] bytes = new byte[(int) getFile().getSize()];
            while ((reader = in.read(bytes)) != -1) {
                out.write(bytes, 0, reader);

            }
            in.close();
            out.flush();
            out.close();
            cell1=obj.leerArchivoExcel(destination + fileName);
        } catch (IOException ex) {
            Logger.getLogger(FileUpLoadControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
